﻿using AiFrame.Base.Core.MVP;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using AiFrame.InterfaceLib.MVP;
using Moq;

namespace Base.Core.Tests
{
    
    
    /// <summary>
    ///Dies ist eine Testklasse für "TabSetManagerTest" und soll
    ///alle TabSetManagerTest Komponententests enthalten.
    ///</summary>
    [TestClass()]
    public class TabSetManagerTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Ruft den Testkontext auf, der Informationen
        ///über und Funktionalität für den aktuellen Testlauf bietet, oder legt diesen fest.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Zusätzliche Testattribute
        // 
        //Sie können beim Verfassen Ihrer Tests die folgenden zusätzlichen Attribute verwenden:
        //
        //Mit ClassInitialize führen Sie Code aus, bevor Sie den ersten Test in der Klasse ausführen.
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Mit ClassCleanup führen Sie Code aus, nachdem alle Tests in einer Klasse ausgeführt wurden.
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Mit TestInitialize können Sie vor jedem einzelnen Test Code ausführen.
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Mit TestCleanup können Sie nach jedem einzelnen Test Code ausführen.
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///Ein Test für "TabSetManager-Konstruktor"
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Base.Core.dll")]
        public void TabSetManagerConstructorTest()
        {
            TabSetManager_Accessor target = new TabSetManager_Accessor();
            Assert.IsNotNull(target._windowCollection);
        }

        /// <summary>
        ///Ein Test für "BringWindowToFront"
        ///</summary>
        public void BringWindowToFrontTestHelper<W>()
        {
            var windowMock = new Mock<IWindow>();

            TabSetManager_Accessor target = new TabSetManager_Accessor();
            var mainWindowMock = new Mock<IMainWindowView>();
            target.Initialize(mainWindowMock.Object);
            target.RegisterWindow(windowMock.Object);
            target.BringWindowToFront<W>();
            
            windowMock.Verify(w=>w.BringWindowToFront());

        }

        [TestMethod()]
        public void BringWindowToFrontTest()
        {
            BringWindowToFrontTestHelper<GenericParameterHelper>();
        }

        /// <summary>
        ///Ein Test für "BringWindowToFront"
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Base.Core.dll")]
        public void BringWindowToFrontTest1()
        {
            var windowMock = new Mock<IWindow>();

            TabSetManager_Accessor target = new TabSetManager_Accessor();
            var mainWindowMock = new Mock<IMainWindowView>();
            target.Initialize(mainWindowMock.Object);
            target.RegisterWindow(windowMock.Object);
            target.BringWindowToFront(windowMock.Object);

            windowMock.Verify(w => w.BringWindowToFront());
        }

        /// <summary>
        ///Ein Test für "CloseWindow"
        ///</summary>
        [TestMethod()]
        public void CloseWindowTest()
        {
            var windowMock = new Mock<IWindow>();

            TabSetManager_Accessor target = new TabSetManager_Accessor();
            var mainWindowMock = new Mock<IMainWindowView>();
            target.Initialize(mainWindowMock.Object);
            target.RegisterWindow(windowMock.Object);
            target.CloseWindow(windowMock.Object);

            windowMock.Verify(w=>w.SetParentWindow(null));
            windowMock.Verify(w => w.SetFree());
            Assert.IsFalse(target.ExistsWindow<IWindow>());
        }

        /// <summary>
        ///Ein Test für "ExistsWindow"
        ///</summary>
        public void ExistsWindowTestHelper<W>()
        {
            var windowMock = new Mock<IWindow>();

            TabSetManager_Accessor target = new TabSetManager_Accessor();
            var mainWindowMock = new Mock<IMainWindowView>();
            target.Initialize(mainWindowMock.Object);
            target.RegisterWindow(windowMock.Object);
            Assert.IsTrue(target.ExistsWindow<IWindow>());

            target.CloseWindow(windowMock.Object);
            Assert.IsFalse(target.ExistsWindow<IWindow>());
        }

        [TestMethod()]
        public void ExistsWindowTest()
        {
            ExistsWindowTestHelper<GenericParameterHelper>();
        }

        /// <summary>
        ///Ein Test für "GetWindowTypeIndex"
        ///</summary>
        public void GetWindowTypeIndexTestHelper<W>()
        {
            var windowMock = new Mock<IWindow>();

            TabSetManager_Accessor target = new TabSetManager_Accessor();
            var mainWindowMock = new Mock<IMainWindowView>();
            target.Initialize(mainWindowMock.Object);
            target.RegisterWindow(windowMock.Object);

            var actualIndex = target.GetWindowTypeIndex<IWindow>();
            Assert.AreEqual(0, actualIndex);
        }

        [TestMethod()]
        [DeploymentItem("Base.Core.dll")]
        public void GetWindowTypeIndexTest()
        {
            GetWindowTypeIndexTestHelper<GenericParameterHelper>();
        }

        /// <summary>
        ///Ein Test für "Initialize"
        ///</summary>
        [TestMethod()]
        public void InitializeTest()
        {
            var windowMock = new Mock<IWindow>();

            TabSetManager_Accessor target = new TabSetManager_Accessor();
            var mainWindowMock = new Mock<IMainWindowView>();
            target.Initialize(mainWindowMock.Object);

            Assert.AreEqual(mainWindowMock.Object, target._mainWindowView);
        }

        /// <summary>
        ///Ein Test für "Instance"
        ///</summary>
        [TestMethod()]
        public void InstanceTest()
        {
            var actualInstance = TabSetManager.Instance();
            Assert.IsNotNull(actualInstance);
        }
        

        /// <summary>
        ///Ein Test für "OnActiveWindowChanged"
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Base.Core.dll")]
        public void OnActiveWindowChangedTest()
        {
            var windowMock = new Mock<IWindow>();

            TabSetManager target = TabSetManager.Instance();
            var mainWindowMock = new Mock<IMainWindowView>();
            target.Initialize(mainWindowMock.Object);

            bool called = false;
            target.ActiveWindowChanged += delegate { called = true; };
            target.RegisterWindow(windowMock.Object);
            Assert.IsTrue(called);

            // 2. Test
            called = false;
            target.BringWindowToFront<IWindow>();
            Assert.IsTrue(called);
            
        }

        /// <summary>
        ///Ein Test für "OnActiveWindowChanges"
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Base.Core.dll")]
        public void OnActiveWindowChangesTest()
        {
            var windowMock = new Mock<IWindow>();

            TabSetManager target = TabSetManager.Instance();
            var mainWindowMock = new Mock<IMainWindowView>();
            target.Initialize(mainWindowMock.Object);

            bool activeWindowChangesCalled = false;
            bool activeWindowChangedCalled = false;

            target.ActiveWindowChanges += delegate
                                              {
                                                  Assert.IsFalse(activeWindowChangedCalled);
                                                  activeWindowChangesCalled = true;
                                              };
            target.ActiveWindowChanged += delegate
                                              {
                                                  activeWindowChangedCalled = true;
                                                  Assert.IsTrue(activeWindowChangesCalled);
                                              };

            target.RegisterWindow(windowMock.Object);
            Assert.IsTrue(activeWindowChangesCalled);

            // 2. Test
            activeWindowChangesCalled = false;
            activeWindowChangedCalled = false;
            target.BringWindowToFront<IWindow>();
            Assert.IsTrue(activeWindowChangesCalled);
        }

        /// <summary>
        ///Ein Test für "RefreshView"
        ///</summary>
        [TestMethod()]
        public void RefreshViewTest()
        {
            var windowMock = new Mock<IWindow>();

            TabSetManager target = TabSetManager.Instance();
            var mainWindowMock = new Mock<IMainWindowView>();
            target.Initialize(mainWindowMock.Object);
            target.RegisterWindow(windowMock.Object);
            
            target.RefreshView();
            windowMock.Verify(w=>w.RefreshView());
        }
        

        /// <summary>
        ///Ein Test für "ActiveWindow"
        ///</summary>
        [TestMethod()]
        public void ActiveWindowTest()
        {
            var windowMock = new Mock<IWindow>();

            TabSetManager target = TabSetManager.Instance();
            var mainWindowMock = new Mock<IMainWindowView>();
            target.Initialize(mainWindowMock.Object);
            target.RegisterWindow(windowMock.Object);

            Assert.AreEqual(windowMock.Object, target.ActiveWindow);
        }
    }
}
